import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import 'amfe-flexible'
import store from './store'
import Vant from 'vant'
import 'vant/lib/index.css'
import qs from 'qs'

axios.defaults.baseURL = 'http://120.79.2.254:8030'
Vue.prototype.$http = axios
Vue.config.productionTip = false

Vue.use(Vant)

// 结合keep-alive 实现记录列表页滚动条位置
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  // 要离开页面如果设置为需要缓存，则本页是要记住上滚动高度到vuex中，以便下次进来恢复高度
  if (from.meta.keepAlive == true) {
    store.commit('recordScroll', document.documentElement.scrollTop || document.body.scrollTop) // document.body.scrollTop一定要加不然iOS上会失效，本人亲测，踩坑
  }
  next()
})
router.afterEach((to, from) => {
  // 如果进入后的页面不想需要缓存，则设置scrollTop = 0
  if (to.meta.keepAlive == false) {
    // console.log('不需要缓存')
    setTimeout(() => {
      document.documentElement.scrollTop = 0
      document.body.scrollTop = 0 // document.body.scrollTop一定要加不然iOS上会失效，本人亲测，踩坑
    }, 0)
  } else {
    // console.log('需要缓存' + store.state.scrollTop)
    setTimeout(() => {
      document.documentElement.scrollTop = store.state.scrollTop
      document.body.scrollTop = store.state.scrollTop // document.body.scrollTop一定要加不然iOS上会失效，本人亲测，踩坑
    }, 0)
  }
})

new Vue({
  router,
  render: h => h(App),
  store
}).$mount('#app')
